﻿package Interface
{
	import com.junkbyte.console.Cc;
	
	import Core.Game;
	import Core.InterfacePage;
	import GameUtils.TrackData;
	import GameUtils.BattleRequestData;
	
	import Interface.BattlesClasses.BattlesScroller;
	import Interface.BattlesClasses.BattleLabel;
	
	import Network.TCPConstants;
	import Network.TCPMessageEvent;			
	import Network.DataMessage;
	
	import flash.events.MouseEvent;	
	import flash.events.Event;	
	import flash.text.TextField;
	import Interface.MessagesClasses.TrackLoadingMessage;
	
	public class PageBattles extends InterfacePage
	{
		private var FBattleExist:Boolean;
		private var FCurrentBattleData:TrackData;
		private var FScroller:BattlesScroller;		
		private var FInfo:TextField;
		
		public function PageBattles():void
		{			
			FBattleExist = false;
			FScroller = getChildByName('BattlesListSroller') as BattlesScroller;
			FInfo = getChildByName('InfoText') as TextField;			
		}
		
		override public function set GameCore(AGame:Game):void
		{
			super.GameCore = AGame;
			FGame.MultiplayerClient.addEventListener(Event.CONNECT, OnConnect);			
		}
		
		private function OnConnect(e:Event):void
		{
			FGame.MultiplayerClient.SendBattlesListRequest();			
			AddBots();
		}
		
		public function set BattlesList(AList:String):void
		{
			FScroller.Clear();
			AddBots();
			
			var vItems:Array = AList.split(';');
			vItems.pop();
			var vData:BattleRequestData;
			
			for(var i in vItems)
			{
				var vBattleInfo:Array = vItems[i].toString().split(',');
				vData = new BattleRequestData();
				vData.UID = vBattleInfo[0];
				vData.UserName = vBattleInfo[1];
				vData.Rating = int(vBattleInfo[2]);
				vData.Track = FGame.Tracks.TrackByID(int(vBattleInfo[3]));
				with(FScroller.Add(vData))
					addEventListener(MouseEvent.MOUSE_DOWN, OnBattleLabel);
			}								
		}
		
		//External battle - can be called on TCP client
		public function AddBattle(AMessage:DataMessage):void
		{
			Cc.log('New battle from: ' + AMessage.Message);
			
			var vUserInfo:Array = AMessage.Message.split(',');
			var vData:BattleRequestData = new BattleRequestData();
			vData.UID = vUserInfo[0];
			vData.UserName = vUserInfo[1];
			vData.Rating = AMessage.Points;
			vData.Track = FGame.Tracks.TrackByID(AMessage.Index);			
			
			if(vData.UID == FGame.UserID)
				vData.UserName = 'Ваша битва';
				
			with(FScroller.Add(vData))
				addEventListener(MouseEvent.MOUSE_DOWN, OnBattleLabel);
		}		
		
		public function RemoveBattle(AUID:String):void
		{
			Cc.log('Battle removed from: ' + AUID);
			
			FScroller.Remove(AUID);	
			if(AUID == FGame.UserID)
			{
				FBattleExist = false;
				if(FCurrentBattleData)
				{
					CreateBattle(FCurrentBattleData);
					FCurrentBattleData = null;
				}
			}
		}
		//handler of labels click
		private function OnBattleLabel(e:MouseEvent):void
		{
			var vLabel:BattleLabel = e.target.parent as BattleLabel;			
			if(vLabel.UID == FGame.UserID)
			{
				FGame.MultiplayerClient.SendRemoveBattle();
			}
			else
			{
				if(vLabel.Track.IsLoaded)
				{
					FGame.MultiplayerClient.SendBattleAccept(vLabel.Track.ID, vLabel.UID);
					FInfo.text = 'Перед началом боя убедитесь, что Вы загрузили нужный трек';
				}
				else
				{
					var vMessage:TrackLoadingMessage = FGame.ShowTrackLoading(vLabel.Track);					
					vMessage.YesButton.addEventListener(MouseEvent.MOUSE_DOWN, OnLoadTrack);					
					FInfo.text = 'Трек "' + vLabel.Track.Name + '" не загружен';
				}
			}
		}
				
		//Called from tracks page
		public function CreateBattle(AData:TrackData):void
		{
			if(!FBattleExist)
			{
				FGame.MultiplayerClient.SendNewBattle(AData.ID);
				FBattleExist = true;
			}
			else
			{
				FCurrentBattleData = AData;				
				FGame.MultiplayerClient.SendRemoveBattle();
			}
		}
		
		private function AddBots():void
		{
			for(var i:int = 0; i < FGame.Tracks.All.length; i++)
			{
				if(FGame.Tracks.All[i].Bots.length > 0)
				{
					for(var j in FGame.Tracks.All[i].Bots)
					{
						var vData:BattleRequestData = new BattleRequestData();
						vData.UID = '0';
						vData.UserName = FGame.Tracks.All[i].Bots[j];
						vData.Rating = i;
						vData.Track = FGame.Tracks.All[i];				
						with(FScroller.Add(vData))
						{
							IsBot = true;
							addEventListener(MouseEvent.MOUSE_DOWN, OnBotBattleLabel);
						}
					}
				}
			}
			
			FScroller.Update();
		}
		
		private function OnBotBattleLabel(e:MouseEvent):void
		{
			var vLabel:BattleLabel = e.target.parent as BattleLabel;
			if(vLabel.Track.IsLoaded)
			{				
				FGame.StartBotBattle(vLabel.UID, vLabel.Track);
				FInfo.text = 'Перед началом боя убедитесь, что Вы загрузили нужный трек';
			}
			else
			{
				var vMessage:TrackLoadingMessage = FGame.ShowTrackLoading(vLabel.Track);					
				vMessage.YesButton.addEventListener(MouseEvent.MOUSE_DOWN, OnLoadTrack);					
				FInfo.text = 'Трек "' + vLabel.Track.Name + '" не загружен';
			}			
		}
		
		private function OnLoadTrack(e:MouseEvent):void
		{
			FGame.HideMessage();
			FGame.CurrentPage = FGame.Tracks;			
			FGame.Tracks.Load((e.target.parent.parent as TrackLoadingMessage).Track);
			FInfo.text = 'Перед началом боя убедитесь, что Вы загрузили нужный трек';
		}
	}	
}